package com.chance.cc.crawler.development.bootstrap.douyin;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2021/6/2 0002 10:14
 */
public class DYDevices {
    public static void main(String[] args) throws IOException {
//        devicesObtain();
        deviceResults();
    }

    private static void deviceResults() throws IOException {
        List<String> lines = IOUtils.readLines(new FileInputStream("D:\\chance\\soft\\douyin\\devices\\devices_06-18.txt"), "utf-8");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\chance\\soft\\douyin\\devices\\devices_result_0618.txt");
        List<String> results = new ArrayList<>();
        List<String> device = new ArrayList<>();
        for (String line : lines) {
            if (line.startsWith("----")){
                if (device.size() == 3){
                    String deviceInfo = String.join("\t", device);
                    results.add(deviceInfo);
                } else {
                    System.out.println(JSON.toJSONString(device));
                }
                device.clear();
            } else {
                device.add(line);
            }
        }
        IOUtils.writeLines(results,"\r\n",fileOutputStream,"utf-8");
    }

    private static void devicesObtain() throws IOException {
        List<String> lines = IOUtils.readLines(new FileInputStream("C:\\Users\\Administrator\\Desktop\\jar\\org\\dy_device.log"), "utf-8");
        List<String> results = new ArrayList<>();
        for (String line : lines) {
            Map map = JSON.parseObject(line, Map.class);
            String mc = map.get("mc").toString();
            String openudid = map.get("openudid").toString();
            String build_serial = map.get("build_serial").toString();
            results.add(StringUtils.joinWith("\t",build_serial,openudid,mc));
        }
        IOUtils.writeLines(results,"\r\n",new FileOutputStream("crawler-dev-bootstrap/src/main/java/com/chance/cc/crawler/development/bootstrap/douyin/chushi.txt"),"utf-8");
    }


}
